from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.views.generic import list_detail

from hosting.databases.models import Database
from hosting.databases.forms import AddDatabaseForm
from hosting.profiles.models import Profile
from django.core.urlresolvers import reverse
from hosting.system.user import generate_password

@login_required
def list_databases(request):
    extra_context = { 'form': AddDatabaseForm() }
    if "status" in request.session:
        extra_context['status'] = request.session.get("status")
        extra_context['message'] = request.session.get("message")
        del request.session["status"]
        del request.session["message"]
    return list_detail.object_list(
        request,
        queryset = Database.objects.filter(user=request.user) ,
        template_object_name = 'databases',
        extra_context = extra_context,
    )

@login_required
def add_database(request):
    user = request.user
    if request.method == 'POST':
        form = AddDatabaseForm(request.POST)
        if form.is_valid():
            user_profile = Profile.objects.get(user=user)
            name = form.cleaned_data['name']
            username = user.username+"-"+name
            password = generate_password()
            if user_profile is None:
                request.session["status"] = False
                request.session["message"] = "Database "+name+" can't added. No tarif"
            else:
                if user_profile.databases_used < user_profile.plan.databases_max:
                    database = Database(user=user,
                                host=user_profile.databases_host,
                                name=name,
                                username=username,
                                password=password
                                )
                    database.save()
                    request.session["status"] = True
                    request.session["message"] = "Database "+name+" added"
                    user_profile.databases_used += 1
                    user_profile.save()
                else:
                    request.session["status"] = False
                    request.session["message"] = "Database "+name+" can't added. Max database reached"
    return HttpResponseRedirect(reverse(list_databases))

@login_required
def delete_database(request, database_id):
    user = request.user
    delete_database = Database.objects.get(user=user,pk=database_id)
    user_profile = Profile.objects.get(user=user)
    if request.method == 'POST':
        request.session["status"] = True
        request.session["message"] = "Database "+delete_database.name+" deleted"
        delete_database.delete()
        user_profile.databases_used -= 1
        user_profile.save()
    return HttpResponseRedirect(reverse(list_databases))

@login_required
def change_password(request, database_id):
    user = request.user
    update_database = Database.objects.get(user=user,pk=database_id)
    request.session["status"] = True
    request.session["message"] = "New password for database "+update_database.name+" generated"
    update_database.password = generate_password()
    update_database.save()
    return HttpResponseRedirect(reverse(list_databases))
